page.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. "use client";
  2. import {
  3. PageContainer,
  4. ProDescriptions,
  5. ProTable,
  6. ProCard,
  7. ProForm,
  8. ProFormText,
  9. ProSkeleton,
  10. } from "@ant-design/pro-components";
  11. import type { ProColumns } from "@ant-design/pro-components";
  12. import { Divider, message, Flex, Button } from "antd";
  13. import { fetchApi } from "@/app/_modules/func";
  14. import { useRouter } from "next/navigation";
  15. import { useEffect, useState } from "react";
  16. export default function UserAuth({ params }: { params: { userid: string } }) {
  17. const { push } = useRouter();
  18. //用户信息
  19. const [user, setUser] = useState<any>({});
  20. //角色信息
  21. const [roles, setRoles] = useState([]);
  22. const getUserData = async () => {
  23. const body = await fetchApi(
  24. `/api/system/user/authRole/${params.userid}`,
  25. push
  26. );
  27. if (body !== undefined) {
  28. if (body.code == 200) {
  29. setUser(body.user);
  30. setRoles(body.roles);
  31. setSelectedRowKeys(body.user.roles.map((item: any) => item.roleId));
  32. }
  33. }
  34. };
  35. useEffect(() => {
  36. getUserData();
  37. }, []);
  38. //表格列定义
  39. const columns: ProColumns[] = [
  40. {
  41. title: "序号",
  42. dataIndex: "index",
  43. valueType: "indexBorder",
  44. width: 48,
  45. },
  46. {
  47. title: "角色编号",
  48. dataIndex: "roleId",
  49. search: false,
  50. },
  51. {
  52. title: "角色名称",
  53. dataIndex: "roleName",
  54. search: false,
  55. },
  56. {
  57. title: "权限字符",
  58. dataIndex: "roleKey",
  59. search: false,
  60. },
  61. {
  62. title: "创建时间",
  63. dataIndex: "createTime",
  64. },
  65. ];
  66. //选中行操作
  67. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  68. const rowSelection = {
  69. onChange: (newSelectedRowKeys: React.Key[]) => {
  70. setSelectedRowKeys(newSelectedRowKeys);
  71. },
  72. };
  73. //当前每页条数
  74. const defaultPageSize = 10;
  75. //提交按钮加载状态
  76. const [confirmLoding, setConfirmLoading] = useState(false);
  77. //更新用户角色
  78. const updateAuth = async () => {
  79. setConfirmLoading(true);
  80. const queryParam = {
  81. userId: user.userId,
  82. roleIds: selectedRowKeys.join(","),
  83. };
  84. const body = await fetchApi(
  85. `/api/system/user/authRole?${new URLSearchParams(queryParam)}`,
  86. push,
  87. {
  88. method: "PUT",
  89. }
  90. );
  91. if (body !== undefined) {
  92. if (body.code == 200) {
  93. message.success("授权成功");
  94. } else {
  95. message.error(body.msg);
  96. }
  97. }
  98. setConfirmLoading(false);
  99. };
  100. return (
  101. <PageContainer
  102. header={{
  103. title: "分配角色",
  104. onBack(e) {
  105. push("/system/user");
  106. },
  107. }}
  108. >
  109. {Object.keys(user).length === 0 ? (
  110. <ProSkeleton type="list" />
  111. ) : (
  112. <>
  113. <ProCard title="基本信息">
  114. <ProDescriptions column={24}>
  115. <ProDescriptions.Item span={12} label="用户昵称">
  116. {user.nickName}
  117. </ProDescriptions.Item>
  118. <ProDescriptions.Item span={12} label="用户名称">
  119. {user.userName}
  120. </ProDescriptions.Item>
  121. </ProDescriptions>
  122. </ProCard>
  123. <Divider />
  124. <ProCard title="角色信息">
  125. <ProTable
  126. rowKey="roleId"
  127. rowSelection={{
  128. selectedRowKeys,
  129. ...rowSelection,
  130. }}
  131. tableAlertRender={false}
  132. columns={columns}
  133. dataSource={roles}
  134. pagination={{
  135. defaultPageSize: defaultPageSize,
  136. showQuickJumper: true,
  137. showSizeChanger: true,
  138. // onChange: pageChange,
  139. }}
  140. search={false}
  141. dateFormatter="string"
  142. toolbar={{
  143. actions: [],
  144. settings: [],
  145. }}
  146. />
  147. </ProCard>
  148. <ProCard>
  149. <Flex justify="center" gap="middle">
  150. <Button href="/system/user">返回</Button>
  151. <Button
  152. type="primary"
  153. onClick={updateAuth}
  154. loading={confirmLoding}
  155. >
  156. 提交
  157. </Button>
  158. </Flex>
  159. </ProCard>
  160. </>
  161. )}
  162. </PageContainer>
  163. );
  164. }